#!/bin/bash

__FileName__=""
__Author__="SuoSuo"
__Time__="2023/03/17"
__Version__="V1.0"


#显示程序的版本号和更新日期
version () {
	printf "\033[34m
=============================================================
*   ${PROGRAM_NAME} ${PROGRAM_VERSION}
*   ${AUTHOR_NAME}          ${AUTHOR_NUMBER}
=============================================================
Release Note:
    >>V1.0 20230317

---------------------------------------------------------------
    \033[0m\n"
	exit 0
}

format_centre_print () {
:<<!
para 1: 要打印的字符串
para 2: 占位数
para 3:
para 4: 是否换行，默认否 0/1
!
    Ss=$1
    Le=$2
    Nn=$(( (Le-${#Ss}) / 2 ))
    [ $Nn -lt 0 ] && Nn=0
    pre=$Nn
    suf=$Nn
    fff=$3
    [[ -n "$3" && "$3" -ge 1 ]] && suf=$((suf - fff))
    format_str="%${pre}s%s%${suf}s"
    [[ "$4" -eq 1 ]] && format_str=${format_str}"\n"
    printf "\033[34m${format_str}\033[0m"  "" "${Ss}" ""
#STR="eeeesfsfssfseee"; printf 'ddd %11s%-11s dddd \n' `echo $STR | cut -c 1-$((${#STR}/2))` `echo $STR | cut -c $((${#STR}/2+1))-${#STR}`
}
title_print () {
    while read line
    do
        name=`echo $line | awk '{print $1}' | xargs`
        len=`echo $line | cut -d' ' -f2 | xargs`
        suff=`echo $line | cut -d' ' -f3 | xargs`
        format_centre_print $name $len $suff
    done <<< $1
}

# 磁盘媒介判断
disk_medium_judge () {
:<<!
para 1: disk name
para 2: ROTA
!
    if [[ $2 -eq 1 ]]; then
        echo "HDD" ; return 0
    else
        if [[ $1 =~ nvme ]]; then
            echo "NVME"; return 0
        elif [[ $2 -eq  0 ]]; then
            echo "SSD"; return 0
        else
            echo "NA"; return 1
        fi
    fi
}

# 磁盘协议判断
disk_procotol_judge () {
:<<!
para 1: disk name
!
    prefix="smartctl -i /dev/${1} | "
    res=None
    local cmd_Sata="${prefix} awk '/^SATA Version is/{i=4;while (i<=NF) {print \$i; i++}}' | xargs"
    local cmd_Sas="${prefix} awk '/^Transport protocol.*SAS/{i=4;while (i<=NF) {print \$i; i++}}' | xargs"
    local cmd_Nvme="${prefix} egrep '^PCI Vendor/Subsystem ID'  | xargs"
    local cmd_FC="${prefix} egrep -i '^Transport protocol.*Fibre' | xargs"
    cmd_li=("${cmd_Sata}" "${cmd_Sas}"  "${cmd_Nvme}" "${cmd_FC}")
    cmd_li_len=${#cmd_li[@]}
    for ii in `seq 0 $((cmd_li_len - 1))`
    do
        jieguo=$(eval "${cmd_li[${ii}]}")
        if [ -n "${jieguo}" ]; then
            case "${ii}" in
                0) echo "SATA"; return 0; ;;
                1) echo "SAS"; return 0;  ;;
                2) echo "NAME"; return 0; ;;
                3) echo "Fibre"; return 0; ;;
            esac
        fi
    done
    echo "NA"; return 1
}


disk_bus () {
:<<!
HDD
SSD
NVME
虚拟盘

!
    echo
    lsblk -d  -n| grep disk -q
    [[ $? -ne 0 ]] && { printf "\033[31mThe Machine Does Not Have a Disk!!\033[0m\n" ; return; }
    printf "\033[32m                          Disk      \033[0m\n"
    # Formatted output Title
    read -r -d '' titles <<- EOF
NAME      7
STATE     10
MODEL     30  5
SIZE      18  4
MEDIUM    8
PROCO     6
HCTL      10
SCHED     16  4
FwVer     15  3
EOF
    title_print "${titles}"
    if [ ${SHOW_VERBOSE} -ge 1 ];then
        read -r -d '' titles <<- EOF
VENDOR    12 1
FACTOR    12
CACHE     6
SPEED     10
SERIAL    18
EOF
        title_print "${titles}"
    fi
    printf "\n"
    #################################
    for path in `lsscsi -N | grep disk | awk '{print $NF}' | xargs`
    do
        # NAME
        disk_name=`echo $path | cut -d'/' -f3`
        printf "%-8s"  "${disk_name}"
        lsblk_str=`lsblk ${path} -d -n -o 'NAME,STATE,SIZE,HCTL,ROTA,SCHED,RQ-SIZE,WWN,VENDOR,REV'`
        lsblk_str002=`lsblk ${path} -d -n -o 'MODEL,SERIAL'`
        smartctl_str="`smartctl -i ${path} | sed  '/^$/d' | awk '{print $0"==="}' | xargs` "
        # STATE
        printf "%-8s" "`echo ${lsblk_str} | awk '{print $2}' | xargs`"
        # MODEL
        printf "%-30s" "`echo ${smartctl_str} | awk -F':' 'BEGIN{RS="=== "}/^Device Model|^Model Number|^Product/{print $2}'|xargs`"
        # SIZE
        printf "%-9s" "`echo ${lsblk_str} | awk '{print $3}' | xargs`"
        # MEDIUM
        printf "%-6s" "$(disk_medium_judge "${disk_name}" "$(echo ${lsblk_str} | awk '{print $5}' | xargs)")"
        # PROCOTOL
        printf "%-6s" "$(disk_procotol_judge "${disk_name}")"
        # HCTL
        printf "%-12s" "$(echo ${lsblk_str} | awk '{print $4}' | xargs)"
        # SCHEDULER
        printf "%-12s" "$(echo ${lsblk_str} | awk '{print $6}' | xargs)"
        # FW
        printf "%-10s" "`echo ${smartctl_str} | awk -F':' 'BEGIN{RS="=== "}/^Firmware Version|^Revision/{print $2}'|xargs`"
        if [ ${SHOW_VERBOSE} -ge 1 ];then
            # VENDOR
            printf "%-12s" "$(echo ${lsblk_str} | awk '{print $9}' | xargs)"
            # 尺寸
            printf "%-10s" "`echo ${smartctl_str} | awk -F':' 'BEGIN{RS="=== "}/^Form Factor/{print $2}'| awk '{print $1}' | xargs`"
            #

            # SERIAL
            printf "%-20s" "`echo ${smartctl_str} | awk -F':' 'BEGIN{RS="=== "}/^Serial Number|^Serial number/{print $2}'|xargs`"

#        AVAGO, BROADCOM

        fi
        printf "\n"
    done

}



#当前时间，格式是“年.月.日.时.分.秒”
date_time=`date +%Y.%m.%d.%H.%M.%S`
#文件名称
PROGRAM_NAME=$(basename $0)
#作者名字
AUTHOR_NAME="SuoSuo"
#作者工号
AUTHOR_NUMBER="50023"
# 版本
PROGRAM_VERSION="V1.0"
# 首地址
BASE_DIR=`pwd`
SHOW_VERBOSE=0

#显示帮助目录菜单
usage_and_exit () {
    echo
    printf "${PROGRAM_NAME} Version ${PROGRAM_VERSION}\n"
	printf "\\033[32m
    Disk Adapter Information Profile
Usage:
  $PROGRAM_NAME [options]
Options:
    -v           Be Verbose
    -h           Show Help Message
    -b           Show Version
    \n\
Attention:
    1>.
    2>.
Examples:
    ./${PROGRAM_NAME} -v
    ./${PROGRAM_NAME} -vvv
Questions:
    1>.
    2>.   \
    \033[0m\n
"
    exit 1
}

while getopts "vhb" arg
do
	case $arg in
	    v)
	    let SHOW_VERBOSE++ ;;
		h)
		usage_and_exit ;;
		b)
		version ;;
	esac
done

main () {
    disk_bus
}

main